/*
 * 项目名称:    wms-server-jeecg
 * 创建人员:    DongPeng
 * 创建时间:    2023-12-05
 * copyright(c) 2022 无锡星智数服科技有限公司
 */
package org.jeecg.modules.business.job.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.basic.print.entity.PrintConfig;
import org.jeecg.modules.basic.print.entity.WmsDocumentTypeStatus;
import org.jeecg.modules.basic.print.service.IPrintConfigService;
import org.jeecg.modules.basic.print.service.IWmsDocumentTypeStatusService;
import org.jeecg.modules.business.doc.business.entity.WmsInReceipt;
import org.jeecg.modules.business.doc.business.service.IWmsInReceiptService;
import org.jeecg.modules.business.doc.outbound.entity.WmsOutReceipt;
import org.jeecg.modules.business.doc.outbound.service.IWmsOutReceiptService;
import org.jeecg.modules.business.job.service.PurchaseNoticeService;
import org.jeecg.modules.system.service.impl.SysBaseApiImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 过账检查通知
 *
 * @author DongPeng
 * @version 1.0.0
 * @date 2023-12-05
 * @description 类描述
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class PurchaseNoticeServiceImpl implements PurchaseNoticeService {
    private final IWmsDocumentTypeStatusService iWmsDocumentTypeStatusService;
    private final IPrintConfigService iPrintConfigService;
    private final IWmsInReceiptService inReceiptService;
    private final IWmsOutReceiptService iWmsOutReceiptService;
    private final SysBaseApiImpl sysBaseApi;
    private final String PURCHASE = "过账";
    @Override
    public void purchaseNotice() {
        log.info("开始检查一次销售出库任务,如果已经有出库单（单据状态=待提交）,24小时内未跟SAP过账,则钉钉消息提醒仓库经理、仓库文员");
        List<String> noticeList = new ArrayList<>();
        List<WmsDocumentTypeStatus> documentTypeStatusList = iWmsDocumentTypeStatusService.list(new LambdaQueryWrapper<WmsDocumentTypeStatus>()
                .like(WmsDocumentTypeStatus::getButtonText, PURCHASE)
        );
        if (CollectionUtil.isNotEmpty(documentTypeStatusList)){
            List<String> printConfigIdList = documentTypeStatusList.stream().map(WmsDocumentTypeStatus::getPrintConfigId).collect(Collectors.toList());
            List<PrintConfig> printConfigList = iPrintConfigService.list(new LambdaQueryWrapper<PrintConfig>()
                    .in(PrintConfig::getId, printConfigIdList)
            );
            if (CollectionUtil.isNotEmpty(printConfigList)){
                //查询所有配置过账的单据编号,根据单据编号查单据
                List<String> documentTypeList = printConfigList.stream().map(PrintConfig::getDocumentType).distinct().collect(Collectors.toList());
                List<String> inBoundTypeList = documentTypeList.stream().filter(s->s.startsWith("R")).collect(Collectors.toList());
                List<String> outBoundTypeList = documentTypeList.stream().filter(s->s.startsWith("C")).collect(Collectors.toList());
                //查询配置了过账节点的所有待提交的出库单和入库单
                if (CollectionUtil.isNotEmpty(inBoundTypeList)){
                    List<WmsInReceipt> wmsInReceiptList = inReceiptService.list(new LambdaQueryWrapper<WmsInReceipt>()
                            .in(WmsInReceipt::getDocumentType, inBoundTypeList)
                            .eq(WmsInReceipt::getDocumentStatus, "1")
                            .le(WmsInReceipt::getCreateTime, DateUtil.yesterday())
                    );
                    if (CollectionUtil.isNotEmpty(wmsInReceiptList)){
                        wmsInReceiptList.forEach(one->{
                            noticeList.add("请注意: 单据编号:"+one.getDocumentCode()+"的入库单已长时间未提交");
                        });
                    }
                }
                if (CollectionUtil.isNotEmpty(outBoundTypeList)){
                    List<WmsOutReceipt> wmsOutReceiptList = iWmsOutReceiptService.list(new LambdaQueryWrapper<WmsOutReceipt>()
                            .in(WmsOutReceipt::getDocumentType, outBoundTypeList)
                            .eq(WmsOutReceipt::getDocumentStatus, "1")
                            .le(WmsOutReceipt::getCreateTime, DateUtil.yesterday())
                    );
                    if (CollectionUtil.isNotEmpty(wmsOutReceiptList)){
                        wmsOutReceiptList.forEach(one->{
                            noticeList.add("请注意: 单据编号:"+one.getDocumentCode()+"的出库单已长时间未提交");
                        });
                    }
                }
            }
        }
        //获取仓库经理、仓库文员
        if (CollectionUtil.isNotEmpty(noticeList)){

            for (String s : noticeList) {
//                MessageDTO message = new MessageDTO();
//                message.setFromUser();
//                message.setToUser(sysMessage.getEsReceiver());
//                message.setToAll(false);
//                message.setTitle(sysMessage.getEsTitle());
//                message.setContent(s);
//                message.setCategory();
//                message.setTemplateCode();
//                message.setType(sysMessage.getEsType());
//                message.setIsMarkdown();
//                sysBaseApi.sendTemplateMessage();
            }
        }
    }
}
